package org.zhcoms.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.request.SessionScope;
import org.zhcoms.po.User;
import org.zhcoms.service.UserManageService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserManageAction extends ActionSupport {

	private static final long serialVersionUID = -281275682819237996L;

	private User user = new User();

	private UserManageService<User> userManageService;

	private List<User> users;

	private String searchText;

	public String doLogin() {
		List<User> userList = new ArrayList<User>();
			
		User user = new User();
		user.setId(123);
		user.setName("Struts2");
		user.setPassword("123");
		user.setAddress("上海魔都");
		userList.add(user);
		
		User user2 = new User();
		user2.setId(1234);
		user2.setName("Struts4");
		user2.setPassword("1234");
		user2.setAddress("上海魔都44");
		userList.add(user2);
		JSONObject jsonObject = new JSONObject();
		jsonObject.accumulate("user", userList);
		
		// 这里在request对象中放了一个data，所以struts的result配置中不能有type="redirect"
		ServletActionContext.getRequest().setAttribute("data",
				jsonObject.toString());
		return SUCCESS;
		/*System.out.println(getParam("loginInfo"));
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("loginStatus", getParam("loginInfo"));
		ActionContext.getContext().setParameters(params);
		if (this.user.getName() == null || this.user.getPassword() == null)
			return INPUT;
		try {
			User user = userManageService.doLogin(this.user.getName(),
					this.user.getPassword());
			if (user != null) {
				ActionContext.getContext().getSession().put("user", user);
				params = new HashMap<String, Object>();
				params.put("loginStatus", getParam("loginInfo"));
				ActionContext.getContext().setParameters(params);
				return doQuery();
			} else
				return INPUT;
		} catch (Exception e) {
			return ERROR;
		}*/
	}

	public String doQuery() {
		searchText = getParam("queryText");
		users = userManageService.queryUsers(searchText, User.class);
		return SUCCESS;
	}

	public String doAdd() {
		String result = "";
		try {
			String param = getParam("param");
			if (Integer.parseInt(param) > 0) {
				user.setId(0);
				System.out.println("用户名：" + user.getName());
				userManageService.addUser(user);
				result = doQuery();
			} else
				result = "addUser";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public String doEdit() {
		try {
			Integer param = Integer.parseInt(getParam("param"));
			if (param == 0) {
				Integer id = Integer.parseInt(getParam("id"));
				user = userManageService.getUser(User.class, id);
				return "editUser";
			} else if (param == 1) {
				userManageService.modifyUser(user);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doQuery();
	}

	public String doDelete() {
		try {
			Integer param = Integer.parseInt(getParam("id"));
			userManageService.deleteUser(param, User.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doQuery();
	}

	protected String getParam(String key) {
		return ServletActionContext.getRequest().getParameter(key);
	}

	public UserManageService<User> getUserManageService() {
		return userManageService;
	}

	public void setUserManageService(UserManageService<User> userService) {
		this.userManageService = userService;
	}

	public List<User> getUsers() {
		return users;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}
}
